import { getPermiseRoute } from '@/service/modules/user'
import {defineStore} from 'pinia'

/**
 * 菜单按钮缓存
 */
const usePermissionStore = defineStore('permission', {
  state: () => ({
    /**是否已请求菜单权限 */
    hasReqMenuPermission: false,
    /**头部中间部分菜单 */
    headCenterMenu: [] as RouteMenuItem[],
    /**头部右侧菜单 */
    headSideMenu: [] as RouteMenuItem[],
    /**页面侧边栏按钮 */
    sideMenu: [] as RouteMenuItem[],
    /**无权限路由name集合 */
    unPermiseNames: [] as string[],
    permissionMenu: [] as any[],
    /**权限按钮缓存 */
    permiseBtns: {} as any,

  }),
  actions: {
    /**
     * 获取页面权限按钮
     * @param menu_key 菜单key
     */
    async getPermiseBtn (menu_key: string) {
      if (this.permiseBtns[menu_key]) { // 有权限缓存
        return this.permiseBtns[menu_key]
      } else { // 没有权限缓存、请求接口拉取权限按钮
        return []
      }
    },
    async getPermissonMenu () {
      if (!this.hasReqMenuPermission) {
        //TODO 通过接口获取权限数据
        const res:any = await getPermiseRoute()
        this.permissionMenu = res.results
        this.hasReqMenuPermission = true
        return this.permissionMenu
      } else {
        return this.permissionMenu
      }
    }
  }
})

export default usePermissionStore
